package com.yeu.action;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.yeu.model.Evidence;
import com.yeu.util.ConnectionPool;

public class ImageAction extends ActionSupport implements ModelDriven<Evidence>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Map<String, Boolean> boolResult;
	private List<Map<String,String>> result;
	private Evidence image=new Evidence();
	
	public Map<String, Boolean> getBoolResult() {
		return boolResult;
	}
	
	public List<Map<String, String>> getResult() {
		return result;
	}
	
	public Evidence getImage() {
		return image;
	}

	public void setImage(Evidence image) {
		this.image = image;
	}
	
	@Override
	public Evidence getModel() {
		return this.image;
	}
	
	public String add(){
		String sql="insert into IMAGE (IMAGEID,EVENTID,IMAGENAME,IMAGECONTENT) values(IMAGE_SEQUENCE.nextval,?,?,?)";
		boolResult=ConnectionPool.executeUpdate(sql,image.getEventId(),image.getFileFileName(), image.getFile());
		return "boolResult";
	}
	
	public String get() throws Exception{
		List<Map<String, Object>> tmpResult=new ArrayList<Map<String,Object>>();
		String sql="select * from IMAGE where IMAGEID=? and EVENTID=?";
		tmpResult=ConnectionPool.executeStreamQuery(sql, "IMAGECONTENT", image.getId(),image.getEventId());
		for (Map<String, Object> map : tmpResult) {
			image.setFileStream((InputStream)map.get("IMAGECONTENT"));
			image.setFileContentType("image/jpeg");
			image.setFileFileName((String)map.get("IMAGENAME"));
		}
		return INPUT;
	}
	
	public String exist(){
		String sql="select IMAGEID,IMAGENAME from IMAGE where EVENTID=?";
		result=ConnectionPool.executeQuery(sql, image.getEventId());
		return SUCCESS;
	}
	
	public String delete(){
		String sql="delete from IMAGE where EVENTID=? and IMAGEID=?";
		boolResult=ConnectionPool.executeUpdate(sql, image.getEventId(),image.getId());
		return "boolResult";
	}
}
